<?php

namespace common\modules\pms\models\backend;

use common\modules\pms\models\Task;
use common\widgets\ActiveForm;
use common\widgets\DateTimePicker;
use common\widgets\Form;
use common\widgets\Select2;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the form model class for model "Task".
 */
class TaskForm extends Task
{

    /**
     * @inheritdoc
     */
    public $isNewRecord;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return parent::rules();
    }

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => [
                'id',
                'project_id',
                'parent_id',
                'assigner_id',
                'name',
                'label',
                'start_time',
                'end_time',
            ],
            'admin-update' => [
                'id',
                'project_id',
                'parent_id',
                'assigner_id',
                'name',
                'label',
                'start_time',
                'end_time',
            ],
        ];
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/project', 'Task');
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param TaskForm        $model
     * @param mixed           $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        $parentModels = Task::find();
        if ( ! $model->isNewRecord)
        {
            $parentModels->where([
                'project_id' => $this->project_id,
            ]);
        }

        return [
            [
                'columns'    => 2,
                'attributes' => [

                    'id' => [
                        'type'    => Form::INPUT_TEXT,
                        'options' => ['disabled' => true],
                    ],

                    'project_id' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options' => [
                            'options'       => [
                                'multiple'    => false,
                                'placeholder' => Yii::t('backend/pms', 'Select project'),
                            ],
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => ArrayHelper::map(Project::find()->all(), 'id', 'name')
                        ],
                    ],

                    'parent_id' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options' => [
                            'options'       => [
                                'multiple'    => false,
                                'placeholder' => Yii::t('backend/pms', 'Select parent task'),
                            ],
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => ArrayHelper::map(Task::find()
                                ->where([
                                    'project_id' => $this->project_id,
                                ])
                                ->all(), 'id', 'name')
                        ],
                    ],

                    'assigner_id' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'name' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'label' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'start_time' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => DateTimePicker::className(),
                        'options' => [
                            'options' => ['placeholder' => Yii::t('backend/pms', 'Select start time')],
                        ],
                    ],

                    'end_time' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => DateTimePicker::className(),
                        'options' => [
                            'options' => ['placeholder' => Yii::t('backend/pms', 'Select end time')],
                        ],
                    ],

                    'created_at' => [
                        'type'        => Form::INPUT_STATIC,
                    ],

                    'updated_at' => [
                        'type'        => Form::INPUT_STATIC,
                    ],

                ],
            ],
        ];
    }

}
